package com.huabo.sql.page;

/**
 * Created by Jackie Liu on 2017/6/29.
 */
public class OraclePage implements PageSql {

    @Override
    public String getSql(String prefixSql, int pageNum, int pageSize) {
        int min = 0, max = 0, size = 0, num = 0;
        size = pageSize > 0 ? pageSize : 20;
        num = pageNum > 0 ? pageNum : 1;
        min = (num - 1) * size;
        max = min + size;

        return String.format("SELECT * FROM (SELECT ROWNUM AS ROW_NO, T_.* FROM(%s) T_ WHERE ROWNUM <= %d) TABLE_ WHERE TABLE_.ROW_NO > %d", prefixSql, max, min);
        //if (prefixSql.matches(".*(?i)ORDER.*")) {//含有排序
            //String sql = prefixSql.replaceFirst("(?i)SELECT", "SELECT ROWNUM AS _ROWNO");
        //}
    }

}
